package com.fjzn.detect.core.utils.sign;

import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;

import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;

public class RSAUtils {

    public static final String ENCRYPT_TYPE = "RSA";

    /**
     * 公钥加密
     * @param content 要加密的内容
     * @param publicKey 公钥
     */
    public static String encryptPubKey(String content, String publicKey) {
        try{
            RSA rsa = new RSA(null,publicKey);
            return rsa.encryptBase64(content, KeyType.PublicKey);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 私钥解密
     * @param content 要解密的内容
     * @param privateKey 私钥
     */
    public static String decryptPriKey(String content, String privateKey) {
        try {
            RSA rsa = new RSA(privateKey,null);
            return rsa.decryptStr(content, KeyType.PrivateKey);
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 私钥解密
     * @param content 要解密的内容
     * @param privateKey 私钥
     */
    public static byte[] decryptPriKey(byte[] content, String privateKey) {
        try {
            RSA rsa = new RSA(privateKey,null);
            return rsa.decrypt(content, KeyType.PrivateKey);
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 私钥加密
     * @param content 要加密的内容
     * @param privateKey 公钥
     */
    public static String encryptPriKey(String content, String privateKey) {
        try{
            RSA rsa = new RSA(privateKey,null);
            return rsa.encryptBase64(content, KeyType.PrivateKey);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 公钥解密
     * @param content 要解密的内容
     * @param publicKey 公钥
     */
    public static String decryptPubKey(String content, String publicKey) {
        try {
            RSA rsa = new RSA(null,publicKey);
            return rsa.decryptStr(content, KeyType.PublicKey);
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取公私钥-请获取一次后保存公私钥使用
     * @return
     */
    public static RSAKey generateRSAKeyPair() {
        try {
            KeyPair pair = SecureUtil.generateKeyPair(ENCRYPT_TYPE);
            PrivateKey privateKey = pair.getPrivate();
            PublicKey publicKey = pair.getPublic();
            byte[] pubEncBytes = publicKey.getEncoded();
            byte[] priEncBytes = privateKey.getEncoded();

            // 把 公钥和私钥 的 编码格式 转换为 Base64文本 方便保存
            String pubEncBase64 = Base64.encode(pubEncBytes);
            String priEncBase64 = Base64.encode(priEncBytes);

            return new RSAKey(pubEncBase64,priEncBase64);
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static class RSAKey {
        private String publicKey;
        private String privateKey;

        public RSAKey(String publicKey,String privateKey){
            this.privateKey = privateKey;
            this.publicKey = publicKey;
        }

        public String publicKey(){
            return this.publicKey;
        }

        public String privateKey(){
            return this.privateKey;
        }

    }

    public static void main(String[] args){
        String str = "ahdgah";
        RSAKey rsaKey = generateRSAKeyPair();
        System.out.println(rsaKey.privateKey());
        System.out.println(rsaKey.publicKey());
//        String a = encryptPriKey(str,rsaKey.privateKey);
//        System.out.println(a);
//        System.out.println(decryptPubKey(a,rsaKey.publicKey));
    }
}
